//
//  TripsDatabase.m
//  chanyou
//
//  Created by leon on 15/1/9.
//  Copyright (c) 2015年 wanli. All rights reserved.
//

#import "TripsDatabase.h"
#import "TripsModel.h"
#import "DB.h"
#import <ShareSDK/ShareSDK.h>
@implementation TripsDatabase
+ (BOOL)insertIntoTravelWithTripsModel:(TripsModel *)trips
{
    sqlite3 *dbPoint = [DB openDB];
    
    [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
        if (result) {
            NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
            // 注销之后要将此处清除
            [def setObject:[userInfo uid] forKey:@"user"];
        }
    }];
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *userId = [def objectForKey:@"user"];

    NSString *sqlStr = [NSString stringWithFormat:@"insert into Trips(weibo_id,trip_id,trip_name,trip_front_cover_photo_url) values(%ld,%ld,'%@','%@')", [userId integerValue],[trips.id integerValue], trips.name, trips.front_cover_photo_url];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"INSERT OK!");
        return YES;
    } else {
        NSLog(@"INSERT ERROR!");
        return NO;
    }
}
+ (NSInteger)searchWeiboId
{
    sqlite3 *dbPoint = [DB openDB];
    sqlite3_stmt *stmt = nil;
    NSString *sqlStr = @"select weibo_id from Trips";
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);

    NSInteger weiboID = 0;
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            unsigned int weibo = sqlite3_column_int(stmt, 0);
            weiboID = weibo;
        }
    }
    sqlite3_finalize(stmt);
    return weiboID;
}


+ (NSArray *)searchAllTravelsWithWeiboId:(NSInteger)weiboId
{
    sqlite3 *dbPoint = [DB openDB];
    sqlite3_stmt *stmt = nil;
    NSString *sqlStr = [NSString stringWithFormat:@"select trip_id,trip_name,trip_front_cover_photo_url from Trips where weibo_id = %ld", weiboId];
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
//        int weiboId = sqlite3_column_int(stmt, 0);
        int tripId = sqlite3_column_int(stmt, 0);
        const unsigned char *name = sqlite3_column_text(stmt, 1);
        const unsigned char *url = sqlite3_column_text(stmt, 2);
        
        NSString *travelName = [NSString stringWithUTF8String:(const char *)name];
        NSString *travelUrl = [NSString stringWithUTF8String:(const char *)url];
        
        TripsModel *trip = [TripsModel tripsWithId:[NSNumber numberWithInteger:tripId] name:travelName front_cover_photo_url:travelUrl];
        [array addObject:trip];
        }
    }
    sqlite3_finalize(stmt);
    return array;
}

+ (void)deleteFromTrips:(NSInteger)tripsId
{
    sqlite3 *dbPoint = [DB openDB];
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Trips where trip_id = %ld", tripsId];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else {
        NSLog(@"删除失败");
    }
}

+ (void)deleteFromTrips:(NSInteger)tripsId weiboId:(NSInteger)weiboId
{
    sqlite3 *dbPoint = [DB openDB];
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Trips where trip_id = %ld and weibo_id = %ld", tripsId, weiboId];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else {
        NSLog(@"删除失败");
    }
}


@end
